Pt. 1 - Creating a button

By N. Springer

 

This tutorial will teach you how to create a button using xml layouts

 

First we will create a button that exits the program

  1. First, add a button component to your xml file:

 

<Button

  android:layout_width="wrap_content"

  android:layout_height="wrap_content"

  android:text="Exit"

  android:onClick="exitClickHandler"

  android:id="@+id/textButton"

  android:layout_centerVertical = "true"

  android:layout_centerHorizontal = "true"

  android:layout_gravity="center_horizontal" />

 

2. There are a few lines of code that you need to pay attention to:

 

android:text="Exit"

This line sets the text that the button has on it

 

android:onClick="exitClickHandler"

This line determines the function in the java code that the program runs when the button is pressed.

 

3. This means in order to have the button do something, we need to create a function in our java code with the same name that exits the program:

 

public void exitClickHandler(View view){

  finish();

}

The finish() function closes the program

 

This is a fully functional exit button. I recommend playing around with the formatting settings to position the button how you please. This can also be done in the design tab by dragging the button around on the screen.

 

Challenge: Create a button in your program that you created in T01 - TextView and EditText that will set the text in the TextView to whatever the user enters into the EditText (Hint: TextView.setText(String text)). This could be further upgraded to include inputs that change the style, color, and size of the text.

 

Pt. 2 - Button Design: How To

By Boice Wong Rffr

 

This tutorial will walk you through creating a program that allows the customization of buttons in your program. We assume you already have created buttons in a layout to use.

button.JPG

 

 

 

android:background="@drawable/rc"

button2.JPG

 

Challenge: Create a working, non-scientific calculator that has simple functions including basic arithmetic. It should support negative and floating point numbers as well. Use the button formatting methods you learned in pt 2 of this tutorial in order to make your app visually appealing.